*Append all $datasets
clear																			// clear all value labels from memory

use 			ZA1487_recoded.dta												// includes ZA1486 and ZA1487
append 	using 	ZA2809_recoded.dta
append 	using 	ZA3033_recoded.dta
append 	using 	ZA3065_recoded.dta
append 	using 	ZA2820_recoded.dta
append 	using 	ZA2965_recoded.dta
append 	using 	ZA3066_recoded.dta
append 	using 	ZA3861_recoded.dta
append 	using 	ZA4662_recoded.dta
append 	using 	ZA5321_recoded.dta
append 	using 	ZA4586_recoded.dta
append 	using 	ZA5270_recoded.dta



/*Flag variables that indicate different question items/scales: "varname_i" =
 item flag variable, "varname_s" = scale flag variable. */
 
*Create scale and item flag variable for each democracy variable 

foreach var of varlist 	$idea_democracy $libdem_opposition $libdem_freeopinion 	///
						$libdem_partyopportunity $libdem_criticism 				///
						$libdem_conflict $libdem_generalwill {
	gen 	`var'$item 	= 0
	label 	var `var'$item "`var' item flag"
	gen 	`var'$scale = 0
	label 	var `var'$scale "`var' scale flag"
}

*Recode and label flag variables 

*idea_democracy

replace 		$idea_democracy$scale 	= .		if $dataset == "ZA3065"			///
				| $dataset == "ZA4662" | $dataset == "ZA4586" 
label define 	scale_$idea_democracy 0 "Six levels" 
label values	$idea_democracy$scale scale_$idea_democracy


replace			$idea_democracy$item 	= 1 	if $dataset == "ZA1486" 		///
				| $dataset == "ZA1487" | $dataset == "ZA2809" 					/// 
				| $dataset == "ZA3033"
replace 		$idea_democracy$item 	= 2		if $dataset == "ZA2820" 		///
				| $dataset == "ZA2965" | $dataset == "ZA3066" 					///
				| $dataset == "ZA3861"		
replace 		$idea_democracy$item 	= 3		if $dataset == "ZA5321"
replace 		$idea_democracy$item 	= .		if $dataset == "ZA3065" 		///
				| $dataset == "ZA4662" | $dataset == "ZA4586" 

label define 	item_$idea_democracy 0 "Version 1 (2018)" 						///
				1 "Version 2 (as in 1986)" 2 "Version 3 (as in 1995)" 			///
				3 "Version 4 (2005)" 
label values 	$idea_democracy$item item_$idea_democracy

*libdem_opposition & libdem_freeopinion (same respective scales and $datasets)

foreach var of varlist 	$libdem_opposition $libdem_freeopinion {
	replace 			`var'$scale	= 1 if $dataset == "ZA3065" 				///
						| $dataset == "ZA3066"| $dataset == "ZA3861" 			///
						| $dataset == "ZA4662" | $dataset == "ZA5270"
	replace				`var'$scale = . if $dataset == "ZA1486" 				///
						| $dataset == "ZA1487" | $dataset == "ZA2809"			///
						| $dataset == "ZA5321"
	label define 		scale_`var' 0 "6 levels" 1 "5 levels"
	label values 		`var'$scale scale_`var'
	
	replace 			`var'$item	= . if $dataset == "ZA1486" 				///
						| $dataset == "ZA1487" | $dataset == "ZA2809"			///
						| $dataset == "ZA5321"
	label define		item_`var' 0 "Version 1"
	label values 		`var'$item item_`var'
}

*libdem_partyopportunity

replace			$libdem_partyopportunity$scale	= 1 if $dataset == "ZA3066"  	///
				| $dataset == "ZA3861" | $dataset == "ZA5270"

replace			$libdem_partyopportunity$scale	= . if $dataset == "ZA1486"  	///
				| $dataset == "ZA1487" | $dataset == "ZA2809" 					/// 
				| $dataset == "ZA3065" | $dataset == "ZA4662" 					/// 
				| $dataset == "ZA5321"
label define 	scale_$libdem_partyopportunity 0 "6 levels" 1 "5 levels"
label values 	$libdem_partyopportunity$scale scale_$libdem_partyopportunity

replace			$libdem_partyopportunity$item 	= . if $dataset == "ZA1486"  	///
				| $dataset == "ZA1487" | $dataset == "ZA2809" 					///
				| $dataset == "ZA3065" | $dataset == "ZA4662" 					///
				| $dataset == "ZA5321"
label define 	item_$libdem_partyopportunity 0 "Version 1"
label values 	$libdem_partyopportunity$item item_$libdem_partyopportunity

*libdem_criticism & *libdem_generalwill (same respective scales and $datasets)


foreach var of varlist	$libdem_criticism $libdem_generalwill {
	replace 			`var'$scale	= 1 if $dataset == "ZA5270"
	replace				`var'$scale = . if $dataset == "ZA1486" 				///
						| $dataset == "ZA1487" | $dataset == "ZA2809"			///
						| $dataset == "ZA3065" | $dataset == "ZA3066"			/// 
						| $dataset == "ZA3861" | $dataset == "ZA4662" 			///
						| $dataset == "ZA5321"
	label 				define scale_`var' 0 "6 levels" 1 "5 levels"
	label values 		`var'$scale scale_`var'
	
	replace				`var'$item = . if $dataset == "ZA1486" 					///
						| $dataset == "ZA1487" | $dataset == "ZA2809"			///
						| $dataset == "ZA3065" | $dataset == "ZA3066"			/// 
						| $dataset == "ZA3861" | $dataset == "ZA4662" 			///
						| $dataset == "ZA5321"
	label define		item_`var' 0 "Version 1"
	label values 		`var'$item item_`var'
}


*libdem_conflict

replace 		$libdem_conflict$scale	= 1 if $dataset == "ZA3065" 			///
				| $dataset == "ZA5270"  
replace 		$libdem_conflict$scale	= . if $dataset == "ZA1486" 			///
				| $dataset == "ZA1487" | $dataset == "ZA2809"					///
				| $dataset == "ZA3066" | $dataset == "ZA3861"  					///
				| $dataset == "ZA4662" | $dataset == "ZA5321"
				label define 	scale_$libdem_conflict 0 "6 levels" 1 "5 levels"
label values 	$libdem_conflict$scale scale_$libdem_conflict

replace 		$libdem_conflict$item	= . if $dataset == "ZA1486" 			///
				| $dataset == "ZA1487" | $dataset == "ZA2809"					///
				| $dataset == "ZA3066" | $dataset == "ZA3861"  					///
				| $dataset == "ZA4662" | $dataset == "ZA5321"
label define 	item_$libdem_conflict 0 "Version 1"
label values 	$libdem_conflict$item item_$libdem_conflict





*check

foreach var of varlist 	$idea_democracy $libdem_opposition $libdem_freeopinion 	///
						$libdem_partyopportunity $libdem_criticism 				///
						$libdem_conflict $libdem_generalwill {
						tab `var'$scale $dataset, m 
						tab `var'$item $dataset, m
}



save "data_supportdemocracy_ger.dta", replace 
